<template>
	<view>
		<view class="box3" style="margin-top:60rpx;">
				<view class="items">
					<text>手机号：</text>
				  <input class="uni-input" focus placeholder="请输入手机号码"   v-model="submitParams.username"/>
				</view>
				<view class="items" style="margin-top: 30rpx;justify-content: space-between;">
				  <view class="" style="display: flex;justify-content: center;">
				   	<text>验证码：</text>
				    <input class="uni-input input2" placeholder="请输入验证码"  v-model="submitParams.yzm">
				  </view>
				  <view class="yzm" @click="getRegisterYZM(yanzhengma)">{{registerYanzhengma}}</view>
				</view>
				<view class="items" style="margin-top: 30rpx;justify-content: space-between;">
				  <view class="" style="display: flex;justify-content: center;">
				   	<text>密码：</text>
				    <input class="uni-input input2"  placeholder="请输入密码"  v-model="submitParams.password">
				  </view>
				</view>
				<view class="items" style="margin-top: 30rpx;justify-content: space-between;">
				  <view class="" style="display: flex;justify-content: center;">
				   	<text>确认密码：</text>
				    <input class="uni-input input2"  placeholder="请输入密码"  v-model="submitParams.password2">
				  </view>
				</view>
				
				<view class="submt" style="position: relative;left: 0;top: 0;" @click="submt()">
				  <text style="font-size: 40rpx;" class="submitValue">提交</text>
				</view>
		</view>
	</view>
</template>

<script>
	const app = getApp();
	var urlPrefix = app.globalData.urlPrefix;
	export default {
		data() {
			return {
				 yanzhengma: '获取验证码',
				 registerYanzhengma: '获取验证码',
				 submitParams: {
				   username: "",
				   password:"",
				   password2:"",
				   yzm: ""
				 },
			
			}
		},
		methods: {
			getRegisterYZM(yzm) {
				if (this.registerYanzhengma == '获取验证码') {
				  const username = this.submitParams.username
				  if (!/^1[3456789]\d{9}$/.test(username)) {
				      uni.showToast({
				          title: '请输入正确的手机号',
				          icon: 'none'
				      });
				      return;
				  }
					let time = 45
					let timer = setInterval(() => {
						this.registerYanzhengma = `${time--}秒后重试`
						if (time == -1) {
						  clearInterval(timer)
						  this.registerYanzhengma = '获取验证码'
						}
					}, 1000)
				   uni.request({
						url: urlPrefix + '/api/app/appUser/forgotPasswordCode?username=' + this.submitParams.username,
						method: 'GET',
						header: {
							'content-type': 'application/json',
						},
						success: function (res) {
							if(res.data.resp_code == 1){
								uni.showToast({
									title: res.data.resp_msg,
									icon: 'none'
								});
						    }
						}
				  	});
			
				}
			},
			
			submt(){
				const params = this.submitParams
				if(params.username == '' || params.username == null){
					uni.showToast({
						title: '请输入手机号',
						icon: 'none',
						duration: 2000
					})
					return;
				} 		
				
				if(params.password=='' || params.password== null){
					uni.showToast({
						title: '请输入密码',
						icon: 'none',
						duration: 2000
					})
					return;
				}
				if(params.password2=='' || params.password2== null){
					uni.showToast({
						title: '请确认密码',
						icon: 'none',
						duration: 2000
					})
					return;
				}
				if(params.yzm=='' || params.yzm== null){
					uni.showToast({
						title: '请输入验证码',
						icon: 'none',
						duration: 2000
					})
					return;
				}
				
				if(params.password2 != params.password){
					uni.showToast({
						title: '两次密码不一致',
						icon: 'none',
						duration: 2000
					})
					return;
				}
				
				
				uni.request({
					url: urlPrefix + '/api/app/appUser/forgotPassword',
					method: 'POST',
					data: {
						username: params.username,
						yzm: params.yzm,
						password: params.password
					},
					header: {
						'content-type': 'application/json',
					},
					success: function (res) {
						if(res.data.resp_code == 1){
							uni.showToast({
								title: res.data.resp_msg,
								icon: 'none'
							});
						}
						if(res.data.resp_code == 0){
							uni.setStorageSync('username', params.username);
							// 跳转路由
							uni.showToast({
							    title: '提交成功',
							    icon: 'success',
							    duration: 2000
							});
							uni.showToast({
							              title: '重置成功',
							              	//将值设置为 success 或者直接不用写icon这个参数
							              	icon: 'success',
							              	//显示持续时间为 2秒
							              	duration: 2000
							            })
							            var timer = setInterval(()=> {
							              uni.navigateBack()
							              clearInterval(timer)
							            },2000)
						}
					}
				});
			}
		}
	}
</script>

<style lang="scss">
@import './forgotPassword.scss';
</style>
